<?php

/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/30
 * Time: 下午4:43
 */
namespace App\Http\Controllers\Api\UserCart;
use App\Http\Controllers\Api\ApiBaseController;
use App\Http\Model\Bundle AS BundleModel;
use App\Logic\UserCartLogic;
use App\Logic\UserCartsLogic;
use App\Logic\GoodsSkuLogic;
use App\Logic\BundleLogic;
use ApiResultFacade;
use Cache;

class UserCartController extends ApiBaseController
{
    public function myUserCart()
    {

    }

    public function setUserCart()
    {

        $sku_id = $this->_request->input('sku_id');
        $type = $this->_request->input('type');
        $num = $this->_request->input('num');
        $user = $this->_request->user();
        if ($type == UserCartLogic::SKU_TYPE)
        {
            $skuLogic = GoodsSkuLogic::initBySku($sku_id);
            $sku = $skuLogic->getSku();
        }else{
            $bundLogic = BundleLogic::initByBundle($sku_id);
            $sku = $bundLogic->getBundle();
        }

        $userCarts = $user->userCart;
        $userCartsLogic = UserCartsLogic::initByUserCart($userCarts);
        if (!$userCart = $userCartsLogic->checkIshasUserCart($sku_id, $type)) //如果之前的购物车里面没有该数据,则创建新的userCart
        {
            $userCartLogic = UserCartLogic::create($sku, $type, $user);
        }else{
            $userCartLogic = UserCartLogic::initByUserCart($userCart);
        }
        $userCartLogic->setNum($num)->setPrice()->save();
        $userCartsData = $userCartsLogic->loadBundle()->loadGoods()->getUserCart();
        $data = $userCartsLogic->toApi($userCartsData);
        return ApiResultFacade::setData($data)->ApiResult();
        
    }
    
    public function emptCart()
    {
        $sku_id = 73;
        $skuLogic = GoodsSkuLogic::initBySku($sku_id);
        print_r($skuLogic->getSku()->toArray());
        exit();
        $user = $this->_request->user();
        $userCartsLogic = UserCartsLogic::emptyUserCart($user->user_id);
        $userCartsData = $userCartsLogic->loadBundle()->loadGoods()->getUserCart();
        $data = $userCartsLogic->toApi($userCartsData);
        return ApiResultFacade::setData($data)->ApiResult();
    }
}